<?php

require_once dirname(__FILE__)."/../util/Setting.php";

class MyDB {
	protected $MYSQLlink, $MYSQLdebug, $MYSQLserver, $MYSQLuser, $MYSQLpass, $MYSQLdb, $MYSQLstartTime, $MYSQLtotalTime = 0, $MYSQLqry =  Array();

	function __construct($MYSQLserver = delivery_collection_schedule_MYSQL_HOST, $MYSQLuser = delivery_collection_schedule_MYSQL_USER, $MYSQLpass = delivery_collection_schedule_MYSQL_PASS, $MYSQLdb = delivery_collection_schedule_MYSQL_NAME) {
		$this->MYSQLserver = $MYSQLserver;
		$this->MYSQLuser   = $MYSQLuser;
		$this->MYSQLpass   = $MYSQLpass;
		$this->MYSQLdb     = $MYSQLdb;
		$this->connectToServer();
    }

    public function __destruct() {
		$this->closeConn();
    }

    public function closeConn() {
		if(is_resource($this->MYSQLlink))
		{
			mysql_close($this->MYSQLlink);
			$this->MYSQLlink = null;
		}
    }

    private function connectToServer() {
		$this->MYSQLlink = mysql_connect($this->MYSQLserver, $this->MYSQLuser, $this->MYSQLpass);
		if(!$this->MYSQLlink) {
			
			$this->debugData("Can't connect to MySQL server, ERROR" . mysql_error());
		}
		$dbSelect = mysql_select_db($this->MYSQLdb,$this->MYSQLlink);
		if(!$dbSelect) {
			$this->debugData("Can't select db, ERROR: " . mysql_error());
		}
    }

	private function connectToBackup($try){
		if($try < count($backups)){
			$curTry = $backups[$try];

			$this->MYSQLlink = mysql_connect($curTry, $this->MYSQLuser, $this->MYSQLpass);
			if($this->MYSQLlink) {
				return true;
			}else{
				$this->debugData("Can't connect to MySQL server, ERROR" . mysql_error());
				return $this->connectToBackup($try+1);
			}
		}else{
			$this->debugData("All backup mysql connections failed");
			return false;
		}
	}

	private function debugData($information) {
		if(delivery_collection_schedule_MYSQL_DEBUG) {
			echo "<!-- MYSQL $information -->\n";
		}
		$this->MYSQLdebug .= $information . "\n";
	}

	private function timer($action) {
		switch($action) {
			case "start":
				$this->MYSQLstartTime = explode(" ", microtime());
				break;
			case "stop":
				$timeparts = explode(" ", microtime());
				$secDiff = $timeparts[1] - $this->MYSQLstartTime[0];
				$milliDiff = (round(($timeparts[0] * 100000))/100000) - $this->MYSQLstartTime[1];
				$runtime = $secDiff + $milliDiff;
				return $runtime;
				break;
			default:
				break;

		}
	}
	
	public function getRunTime() {
		for($arrayPos = 0; $arrayPos < sizeof($this->MYSQLqry); $arrayPos++) {
			$procent = round(($this->MYSQLqry[$arrayPos][1] / $this->MYSQLtotalTime) * 10000) / 100;
			$this->MYSQLqry[$arrayPos][2] = $procent . "%";
		}
		return $this->MYSQLqry;
	}

	public function getDebug() {
		return $this->MYSQLdebug;
	}

	public function mysqlData($inSQL) {
		if(!$this->MYSQLlink) {
			$this->connectToServer();
		}
		$this->timer('start');
		mysql_query("SET NAMES 'utf8'", $this->MYSQLlink);
		$res = mysql_query($inSQL, $this->MYSQLlink);
		$runtime = $this->timer('stop');
		if(!$res) {
			$this->debugData("Bad query, ERROR: " . mysql_error() . " QUERY:". $inSQL);
		} else {
			$this->MYSQLtotalTime += $runtime;
			$this->MYSQLqry[] = array($inSQL,$runtime);

		}
		return $res;
	}
	
	public function mysqlLastInsertID()
	{
		$oid = mysql_insert_id($this->MYSQLlink);
		return $oid;
	}

	public function mysqlOneField($inSQL) {
		$res = $this->mysqlData($inSQL);
		$outdata = false;
		if($res) {
			if(mysql_num_fields($res) == 1 && mysql_num_rows($res) == 1) {
				$outdata = mysql_result($res,0,0);
			}
		}
		return $outdata;
	}
	
	public function mysqlOneRow($inSQL) {
		$res = $this->mysqlData($inSQL);
		$outdata = false;
		if($res) {
			if(mysql_num_rows($res) == 1) {
				$outdata = mysql_fetch_array($res);
			}
		}
		return $outdata;
	}

	public function mysqlRows($inSQL) {
		$res = $this->mysqlData($inSQL);
		$outdata = array();
		if($res) {
			while ($row = mysql_fetch_array($res))
				$outdata[] = $row;
		}
		return $outdata;
	}

	public function affected_rows(){
		return mysql_affected_rows($this->MYSQLlink);
	}

	public function useDatabase() {
		$dbSelect = mysql_select_db($this->MYSQLdb,$this->MYSQLlink);
		if(!$dbSelect) {
			$this->debugData("Can't select db, ERROR: " . mysql_error());
		}
	}
}
